package com.csun.cmny.provider.service.impl;

import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcServiceComplaintMapper;
import com.csun.cmny.provider.dao.OmcServiceOrderMapper;
import com.csun.cmny.provider.model.constant.ServiceOrderComplaintConstant;
import com.csun.cmny.provider.model.domain.OmcServiceComplaint;
import com.csun.cmny.provider.model.domain.OmcServiceOrder;
import com.csun.cmny.provider.model.dto.ServiceComplaintDto;
import com.csun.cmny.provider.model.dto.comolaint.ServiceComplaintSaveDto;
import com.csun.cmny.provider.model.dto.comolaint.ServiceComplaintSelectDto;
import com.csun.cmny.provider.model.vo.ServiceListComplaintVo;
import com.csun.cmny.provider.model.vo.ServicedOrderVo;
import com.csun.cmny.provider.model.vo.complaint.ServiceComplaintVo;
import com.csun.cmny.provider.service.OmcServiceComplaintService;
import com.csun.cmny.provider.service.OmcServiceOrderService;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Transactional
@Service
public class OmcServiceComplaintServiceImpl extends BaseService<OmcServiceComplaint> implements OmcServiceComplaintService {


    @Resource
    private OmcServiceComplaintMapper complaintMapper;

    @Resource
    private OmcServiceOrderService orderService;
    @Resource
    private OmcServiceOrderMapper omcServiceOrderMapper;

    @Override
    public List<ServiceComplaintVo> selectComplaint(ServiceComplaintSelectDto serviceComplaintSelectDto) {
        return complaintMapper.selectComplaint(serviceComplaintSelectDto);
    }


    @Override
    public PageInfo listComplaint(ServiceComplaintDto serviceComplaintDto) {

        //查询投诉状态
        List<Integer> list = new ArrayList<>();
        list.add(ServiceOrderComplaintConstant.ORDER_COMPLAINT_WAIT);
        list.add(ServiceOrderComplaintConstant.ORDER_COMPLAINT_RECEIVE);
        serviceComplaintDto.setComplaintStatusList(list);


        List<ServiceListComplaintVo> voList = omcServiceOrderMapper.listComplaintOrderPage(serviceComplaintDto);

        List<ServiceListComplaintVo> serviceListComplaintVoList = new ArrayList<>();
        for(ServiceListComplaintVo vo:voList){
            OmcServiceComplaint omcServiceComplaint = complaintMapper.selectByPrimaryKey(vo.getOrderId());
            if(PublicUtil.isEmpty(omcServiceComplaint)||omcServiceComplaint.getTargetOfComplaint()!=0) continue;
            ServiceListComplaintVo serviceListComplaintVo = new ServiceListComplaintVo();
            BeanUtils.copyProperties(vo,serviceListComplaintVo);
            serviceListComplaintVo.setOmcServiceComplaint(omcServiceComplaint);
            serviceListComplaintVoList.add(serviceListComplaintVo);
        }
        return new PageInfo<>(serviceListComplaintVoList);
    }

    @Override
    public int submit(ServiceComplaintSaveDto serviceComplaintSaveDto) {

        //修改订单的投诉状态
        OmcServiceOrder omcServiceOrder = new OmcServiceOrder();
        omcServiceOrder.setId(serviceComplaintSaveDto.getServiceOrderId());
        omcServiceOrder.setComplaintStatus(ServiceOrderComplaintConstant.ORDER_COMPLAINT_WAIT);
        int update = orderService.update(omcServiceOrder);

        //新增投诉信息
        OmcServiceComplaint omcServiceComplaint = new OmcServiceComplaint();
        BeanUtils.copyProperties(serviceComplaintSaveDto,omcServiceComplaint);
        int save = save(omcServiceComplaint);

        return save;
    }
}
